/*
 RLRecyclableItemButtonController.h
 
 Date Created: February 19, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "RLRecyclableItemButton.h"

#import "RLRecyclableType.h"


@implementation RLRecyclableItemButton

@synthesize backgroundIconView;
@synthesize nameLabel;
@synthesize quantityLabel;
@synthesize item;

- (RLRecyclableItemButton *)initWithItem:(RLRecyclableItem *)itemArg {
  self=[super initWithFrame:CGRectMake(0,0,100,100)];
  if( self) {
		// Configure button
		self.backgroundColor=UIColor.brownColor;
		self.contentMode = UIViewContentModeScaleToFill;
		
		// Configure icon
		self.backgroundIconView=[[UIImageView alloc] initWithFrame:self.frame];
		CGRect backgroundIconViewFrame=CGRectMake(0, 0, 0, 0);
		backgroundIconViewFrame.size=self.frame.size; //icon.size;
		backgroundIconView.frame=backgroundIconViewFrame;
		backgroundIconView.center=CGPointMake(self.frame.size.width/2,self.frame.size.height/2);
		backgroundIconView.contentMode = UIViewContentModeScaleAspectFit;
    backgroundIconView.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                          UIViewAutoresizingFlexibleWidth | 
                                          UIViewAutoresizingFlexibleLeftMargin | 
                                          UIViewAutoresizingFlexibleRightMargin | 
                                          UIViewAutoresizingFlexibleTopMargin |
                                          UIViewAutoresizingFlexibleBottomMargin;
    
		// Configure name label
    self.nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(5,60,95,40)];
    self.nameLabel.textAlignment=UITextAlignmentCenter;
    self.nameLabel.numberOfLines=2;
    UIFont *nameFont=[UIFont systemFontOfSize:10];
    self.nameLabel.font=nameFont;
    self.nameLabel.backgroundColor=UIColor.clearColor;
    self.nameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                      UIViewAutoresizingFlexibleWidth | 
                                      UIViewAutoresizingFlexibleLeftMargin | 
                                      UIViewAutoresizingFlexibleRightMargin | 
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin;
    
	  
		// Configure quantity label
    self.quantityLabel=[[UILabel alloc] initWithFrame:CGRectMake(20,10,60,60)];
    self.quantityLabel.textAlignment=UITextAlignmentCenter;
    UIFont *quantityFont=[UIFont boldSystemFontOfSize:36];
    self.quantityLabel.font=quantityFont;
    self.quantityLabel.backgroundColor=UIColor.clearColor;
    self.quantityLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                          UIViewAutoresizingFlexibleWidth | 
                                          UIViewAutoresizingFlexibleLeftMargin | 
                                          UIViewAutoresizingFlexibleRightMargin | 
                                          UIViewAutoresizingFlexibleTopMargin |
                                          UIViewAutoresizingFlexibleBottomMargin;
    
    
    [self addSubview:self.backgroundIconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.quantityLabel];
    
    self.item=itemArg;
  }
  return self;
}

- (void)setItem:(RLRecyclableItem *)newItem {
  if(item) {
    [item removeObserver:self forKeyPath:@"quantity"];
    [item release];
  }
  item=newItem;
  [item retain];

  [item addObserver:self 
         forKeyPath:@"quantity" 
            options:0
            context:nil];
  
  UIImage *icon=[UIImage imageNamed:item.typeDescription.icon];
  backgroundIconView.image=icon;
  
  self.nameLabel.text=[RLRecyclableType typeForUniqueName:self.item.type].name;
  self.quantityLabel.text=[NSString stringWithFormat:@"%d",self.item.quantity];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
  if([keyPath isEqualToString:@"quantity"]) {
    self.quantityLabel.text=[NSString stringWithFormat:@"%d",self.item.quantity];
  }
}

- (void)dealloc {
  [super dealloc];
  [item release];
}


@end
